<?php
    require_once($application_folder."/controllers/navigator.php");
    class Code extends navigator 
    {

        var $data;

        function __construct()
        {
            parent::__construct();
            $this->data['layout_id'] = 1;
            $this->load->library('class_model/cars');
            $this->load->library('class_model/booking');
            $this->load->library('class_model/communicator');
            $this->load->library('class_model/invoice');

        }

        function index()
        { 
            $this->show_code_module();
        }

        function show_code_module(){     

            $this->core_site('code/home', $this->data);

        }

        function cars_browse_fleet(){        

            $dbRet = $this->cars->BrowseFleet();

            if($dbRet){
                $this->data['fleet'] = $dbRet;
                $this->core_site('code/browseFleet', $this->data); 
            }else{

                echo $this->cars->getFlag().":";

                switch($this->cars->getFlag()){
                    case 'error':
                        echo $this->cars->getError();
                        break;
                    case 'numrows':
                        echo $this->cars->getNumrows();
                        break;
                    case 'info':
                        echo $this->cars->getInfo();
                        break;

                }

            }


        }

        function cars_search_fleet(){        

            $d_from = '04.03.2012 08:00';
            $d_to = '05.03.2012 08:00';

            $dbRet = $this->cars->SearchFleet($d_from,$d_to);
            if($dbRet){
                $this->data['fleet'] = $dbRet;
                $this->core_site('code/searchFleet', $this->data);
            }else{

                echo $this->cars->getFlag().":";

                switch($this->cars->getFlag()){
                    case 'error':
                        echo $this->cars->getError();
                        break;
                    case 'numrows':
                        echo $this->cars->getNumrows();
                        break;
                    case 'info':
                        echo $this->cars->getInfo();
                        break;

                }

            }


        }

        function cars_show_acessories(){        

            $c_id = 37;

            $dbRet = $this->cars->ShowAccessories($c_id);

            if($dbRet){
                $this->data['accessories'] = $dbRet;
                $this->core_site('code/showAccessories', $this->data);
            }else{
                echo $this->cars->getFlag().":";

                switch($this->cars->getFlag()){
                    case 'error':
                        echo $this->cars->getError();
                        break;
                    case 'numrows':
                        echo $this->cars->getNumrows();
                        break;
                    case 'info':
                        echo $this->cars->getInfo();
                        break;

                }
            }

        }

        function cars_show_details(){   

            $c_id = 37;

            $dbRet = $this->cars->ShowDetails($c_id);
            if($dbRet){
                $this->data['car_details'] = $dbRet;
                $this->core_site('code/showDetails', $this->data);
            }else{
                echo $this->cars->getFlag().":";

                switch($this->cars->getFlag()){
                    case 'error':
                        echo $this->cars->getError();
                        break;
                    case 'numrows':
                        echo $this->cars->getNumrows();
                        break;
                    case 'info':
                        echo $this->cars->getInfo();
                        break;

                }
            }


        }

        function booking_add_to_booking(){

            $data = array
            (
            'session_id' => '6147e0b72d9893f31856b136783b747a',
            'ip_address' => '78.155.52.2',
            'user_agent' => 'Mozilla/5.0 (Windows NT 6.1; rv:10.0.2) Gecko/2010',
            'last_activity' => '1330799887',
            'source_info' => 'www.novisajt.com',
            'fullfrom' => '08:00',
            'fullto' => '08:00',
            'pickup_date' => '04.03.2012',
            'return_date' => '05.03.2012',
            'pickup_loc_id' => 2,
            'pickup_loc_text' => 'PODGORICA Airport +20 &#8364;',
            'pickup_loc_value' => 20,
            'return_loc_id' => 2,
            'return_loc_text' => 'PODGORICA Airport +20 &#8364;',
            'return_loc_value' => 20,
            'no_days' => 1,
            'day_price' => 55,
            'tot_price' => 55,
            'selCarId' => 30,
            'cust_name' => 'Ivan Kukic',
            'cust_email' => 'ikukic@yahoo.com',
            'cust_phone' => '+382 69 069 982',
            'cust_note' => 'other details',
            'cust_terms' => 1,
            'extras' => Array(1,2)
            );

            $dbRet = $this->booking->AddToBooking($data);
            if($dbRet){
                print_r($dbRet);
                $this->core_site('code/addToBooking', $this->data);
            }else{
                echo $this->booking->getFlag().":";

                switch($this->booking->getFlag()){
                    case 'error':
                        echo $this->booking->getError();
                        break;
                    case 'numrows':
                        echo $this->booking->getNumrows();
                        break;
                    case 'info':
                        echo $this->booking->getInfo();
                        break;

                }
            }               


        }

        function booking_show_order(){        

            $dbRet = $this->booking->ShowOrder(777);

            $order = $this->booking->test_response($dbRet);
            if(!$order){
                echo $this->booking->getFlag().":";

                switch($this->booking->getFlag()){
                    case 'error':
                        echo $this->booking->getError();
                        break;
                    case 'numrows':
                        echo $this->booking->getNumrows();
                        break;
                    case 'info':
                        echo $this->booking->getInfo();
                        break;

                }
                die();
            }

            $order = $this->booking->test_response($dbRet[0]);

            if($order){
                //print_r($order); 
                $this->data['order'] = $order;

                $order_extras = $this->booking->test_response($dbRet[1]);

                if($order_extras){
                    //print_r($order_extras);
                    $this->data['order_extras'] = $order_extras;
                }else{
                    echo $this->booking->getFlag().":";

                    switch($this->booking->getFlag()){
                        case 'error':
                            echo $this->booking->getError();
                            break;
                        case 'numrows':
                            echo $this->booking->getNumrows();
                            break;
                        case 'info':
                            echo $this->booking->getInfo();
                            break;

                    }
                }

                $this->core_site('code/showOrder', $this->data);

            }else{
                echo $this->booking->getFlag().":";

                switch($this->booking->getFlag()){
                    case 'error':
                        echo $this->booking->getError();
                        break;
                    case 'numrows':
                        echo $this->booking->getNumrows();
                        break;
                    case 'info':
                        echo $this->booking->getInfo();
                        break;

                }
            }


        }

        function invoice_preview(){ 



            //Get Data - informacionisistem.com
            $dbRet = $this->invoice->preview(760);

            //Chek if booking exist
            $order = $this->invoice->test_response($dbRet);
            if(!$order){
                echo $this->invoice->getFlag().":";

                switch($this->invoice->getFlag()){
                    case 'error':
                        echo $this->invoice->getError();
                        break;
                    case 'numrows':
                        echo $this->invoice->getNumrows();
                        break;
                    case 'info':
                        echo $this->invoice->getInfo();
                        break;

                }
                die();
            }

            //check firdst row - booking table
            $order = $this->invoice->test_response($dbRet[0]);

            //test for valid json
            if($order){

                //print_r($order[0]);
                //print_r($order[1]);

                $this->data['invoice_no'] = $order[0]->id;
                $this->data['cust_firstName'] = $order[0]->firstName;
                $this->data['cust_email'] = $order[0]->email;
                $this->data['cust_phone'] = $order[0]->phone;

                $this->data['invoice_title'] = $order[0]->carname;

                $this->data['rtcr_invc_vr']  = true;

                $this->data['titles'][0]='Pickup:';
                $this->data['titles'][1]='Return:';

                $this->data['datas'][0]= $order[0]->pickup_loc.' <br /> '.date('d.m.Y / H:s',$order[0]->datefrom).' h';
                $this->data['datas'][1]= $order[0]->return_loc.' <br /> '.date('d.m.Y / H:s',$order[0]->dateto).' h';       

                $this->data['prices'][0]=$order[0]->pickup_loc_price;
                $this->data['prices'][1]=$order[0]->return_loc_price;

                $this->data['price_title'] = 'Rentacar:';
                $this->data['price_desc'] = $order[0]->carname.' - '.$order[0]->numofdays.' day(s)';
                $this->data['price_day_price'] = $order[0]->dayprice;
                $this->data['price_total_price'] = $order[0]->dayprice*$order[0]->numofdays;
                $this->data['total_price'] = $order[0]->totalprice;


                //check secon row - accessories table
                $this->data['cb_numofdays'] = $order[0]->numofdays;

                $order_extras = $this->invoice->test_response($dbRet[1]);

                if($order_extras){
                    //print_r($order_extras);
                    $this->data['accesories'] = $order_extras;
                }else{
                    echo $this->invoice->getFlag().":";

                    switch($this->invoice->getFlag()){
                        case 'error':
                            echo $this->invoice->getError();
                            break;
                        case 'numrows':
                            echo $this->invoice->getNumrows();
                            break;
                        case 'info':
                            echo $this->invoice->getInfo();
                            break;

                    }
                }

                /*$this->data['order'] = $order[0];
                $this->data['order_extras'] = $order[1];*/

                $this->core_site('code/showInvoice', $this->data);  

            }else{
                echo 'Booking '.$this->invoice->getFlag().":";

                switch($this->invoice->getFlag()){
                    case 'error':
                        echo $this->invoice->getError();
                        break;
                    case 'numrows':
                        echo $this->invoice->getNumrows();
                        break;
                    case 'info':
                        echo $this->invoice->getInfo();
                        break;

                }
            }
        }
    }
?>